<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/12 14:25
 * Desc:
 */
trait Trait_Api_Outdoor
{
    /**
     * 获取户外活动首页导航
     * @param $row
     * @return array
     */
    public function nav($row)
    {
        $data = Taglib_Outdoor::attr(array('pid' => 1, 'row' => $row));

        $nav = array();
        foreach ($data['list'] as &$item) {
            $nav[] = array(
                'attrname' => $item['attrname'],
                'id' => $item['id'],
                'litpic' => $this->img_url($item['litpic']),
            );
        }
        return array(
            'list' => $nav
        );
    }

    /**
     * 参与人员
     * @param $id
     * @param $date
     * @return array
     */
    public function join_member($id, $date)
    {
        $members = Model_Outdoor::search_order_date_members($id, $date);
        foreach ($members['list'] as &$v) {
            $v['litpic'] = Model_Api_V2_Common::get_member_header_pic($v['litpic']);
        }
        return $members['list'];
    }

    /**
     * 获取套餐列表
     * @param $params
     * @return array
     * @throws Exception
     */
    public function get_suit($params)
    {

        /**
         * @var $date
         * @var $id
         */
        extract($params);

        $is_seckill   = Arr::get($params, 'is_seckill', 0);
        $card_suit_id = Arr::get($params, 'card_suit_id', 0);
        $is_pintuan   = Arr::get($params, 'is_pintuan', 0);

        if ($is_seckill) {
            $seckill = DB::select()
                ->from('seckill')
                ->where('id', '=', $is_seckill)
                ->execute()
                ->current();
            if (!$seckill) {
                throw new Exception('活动未找到');
            }
            $suit_id = $seckill['suit_id'];
        }

        $query = DB::select(
            'a.*', 'b.need_confirm', 'b.paytype', 'b.suitname', 'b.description', 'b.childdesc',
            'b.paytype', 'b.dingjin', 'b.together_is_open', 'b.together_stock', 'b.together_effect_time',
            'b.together_max_book_num', 'b.refund_restriction'
        )
            ->from(array('outdoor_suit_price', 'a'))
            ->join(array('outdoor_suit', 'b'))
            ->on('a.suitid', '=', DB::expr('sline_b.id'))
            ->where('a.number', '<>', '0')
            ->and_where('a.outdoorid', '=', $id)
            ->and_where('a.day', '=', strtotime($date))
            ->and_where('a.supplier_channel_id', '=', 1);
        if (!empty($suit_id)) {
            $query->and_where('a.suitid', '=', $suit_id);
        }
        if ($card_suit_id) {
            $query->and_where(DB::expr('sline_b.id'), 'IN', explode(',', $card_suit_id));
        }
        $suit = $query->order_by(DB::expr('IFNULL(sline_b.displayorder,9999) ASC'))
            ->execute()
            ->as_array();
        if(!empty($suit)){
            $pay_name = array(
                '1' => '全款支付',
                '2' => '定金支付',
                '3' => '二次确认',
            );
            $refund_class = Service_Refund_Rule_Factory::get_instance(self::TYPE_ID);
            foreach ($suit as $k => &$item) {
                if ($is_pintuan && ($item['need_confirm'] == 1 || $item['paytype'] != 1)) {
                    unset($suit[$k]);
                } else {
                    $item['description'] = $item['description'] ? $this->content_add_domain($item['description']) : '';
                }
                if ($is_seckill) {
                    $item['price']  = $seckill['adult_price'];
                    $item['childprice']  = $seckill['child_price'];
                    $item['number'] = $seckill['purchase_limits'] > 0 && $seckill['number'] > $seckill['purchase_limits'] ? $seckill['purchase_limits'] : $seckill['number'];
                } else {
                    $item['price'] = Currency_Tool::price($item['price']);
                }
                if ($item['number'] == -1 || $item['number'] == '-1') {
                    $item['number'] = 99999999;
                }
                $item['profit']                  = Currency_Tool::price($item['profit']);
                $item['refund_rule']             = $refund_class->get_format_refund_rule($item['suitid']);
                $item['paytype_name']            = $pay_name[$item['paytype']];
                $item['refund_restriction_name'] = $refund_class->get_refund_restriction_name($item['refund_restriction']);
            }
        }

        return $suit;
    }

    /**
     * 合同
     * @param $id
     * @return array|mixed
     */
    public function contract($id)
    {
        $info = DB::select('contractid')
            ->from('outdoor')
            ->where('id', '=', $id)
            ->execute()
            ->current();
        $data = [];
        if ($info && $info['contractid']) {
            $data    = Model_Contract::get_contents($info['contractid'], self::TYPE_ID);
            $url     = St_Functions::get_web_url(0) . '/contract/book_view/contract_id/' . $info['contractid'];
            $content = St_Network::http($url);
            $dir     = BASEPATH . '/uploads/contract/';
            if (!file_exists($dir)) {
                mkdir($dir, 0777);
            }
            $filename = time() . '.html';
            file_put_contents($dir . $filename, $content);

            $download_link   = St_Functions::get_main_host() . '/uploads/contract/' . $filename;
            $data['content'] = $download_link;
        }
        return $data;
    }

    public function recent_outdoor()
    {
        $date = strtotime(date('Ymd'));
        $field = [
            'sline_a.id',
            'sline_a.title',
            'sline_a.litpic',
            'sline_a.lineday',
            'sline_a.linenight',
            'sline_b.day',
            'sline_b.price',
        ];
        $model = DB::select(DB::expr(implode(',', $field)))->from(['outdoor', 'a']);
        $model->join(['outdoor_suit_price', 'b'],'left');
        $model->on('a.id', '=', DB::expr("sline_b.outdoorid"));
        $model->join(['allorderlist', 'c'],'left');
        $model->on('a.id', '=', DB::expr("sline_c.aid"));
        $model->on('c.typeid', '=', 114);
        $model->where('a.ishidden', '=', 0);
        $model->and_where('a.status', '=', 3);
        $model->and_where('a.lineday', '>', 0);
        $model->and_where('b.day', '>=', $date);
        $model->group_by('a.id');
        $model->order_by(DB::expr(" IFNULL(sline_c.displayorder,9999) ASC,sline_a.addtime DESC,sline_a.modtime DESC"));
        $model->limit(3);
        $list = $model->execute()->as_array();
        $uni_img = Model_Uniapp_Images::get_img_list(self::TYPE_ID);
        foreach ($list as &$v) {
            if(isset($uni_img[$v['id']])){
                $v['litpic'] = $uni_img[$v['id']];
            }
            $v['litpic'] = $this->img_url($v['litpic'], 750, 750);
            $v['day']    = date('Y-m-d', $v['day']);
            $v['travel_days'] = $v['lineday'].'天';
            if($v['linenight'] > 0){
                $v['travel_days'] .= $v['linenight'].'晚';
            }
        }

        return $list;
    }
}